/*
 * Copyright 2008 FBK (http://www.fbk.eu/)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.fbk.it.hlt.jlsi.data;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import cern.colt.matrix.DoubleMatrix1D;
import cern.colt.matrix.impl.AbstractMatrix1D;
import cern.colt.matrix.impl.DenseDoubleMatrix1D;

/**
 * Reads in Sparse Binary Matrix File.
 * 
 * @author Claudio Giuliano
 * @version %I%, %G%
 * @since 1.0
 */
public class DenseTextVectorFileReader {
	/**
	 * Define a static logger variable so that it references the Logger instance
	 * named <code>DenseTextVectorFileReader</code>.
	 */
	static Logger logger = Logger.getLogger(DenseTextVectorFileReader.class
			.getName());

	//
	private LineNumberReader lnr;

	/**
	 * The number of rows.
	 */
	private int nr;

	/**
	 * Constructs a sparse binary matrix writer.
	 * 
	 * @param file
	 *            the file where to write the matrix.
	 */
	public DenseTextVectorFileReader(File f) throws IOException {
		lnr = new LineNumberReader(new FileReader(f));

		nr = 0;

		readHeader();

	} // end constructor

	/**
	 * Reads the number of rows, columns and non zero values. All values are
	 * 4-byte integers.
	 */
	private void readHeader() throws IOException {
		String line = lnr.readLine();

		if (line != null)
			;
		{
			nr = Integer.parseInt(line);
		}

		logger.debug("matrix: " + nr);
	} // end readHeader

	/**
	 * Reads the number of non zero values for a column and or each non-zero
	 * value in the column: row index and value. All values are 4-byte integers
	 * except value, which is a 4-byte float. All are in network byte order.
	 * 
	 * @param indexes
	 *            the column indexes.
	 * @param values
	 *            the column values.
	 */
	public AbstractMatrix1D read() throws IOException {
		DoubleMatrix1D matrix = new DenseDoubleMatrix1D(nr);

		for (int i = 0; i < nr; i++) {
		//	System.out.print(i + " ");
			String line = lnr.readLine();
			if (line != null)
				;
			{
				matrix.setQuick(i, (double) Float.parseFloat(line));
			}
		} // end for i

		return matrix;
	} // end read

	//
	public void close() throws IOException {
		lnr.close();
	} // end close

	//
	public static void main(String[] args) throws Exception {
		String logConfig = System.getProperty("log-config");
		if (logConfig == null)
			logConfig = "log-config.txt";

		long begin = System.currentTimeMillis();

		PropertyConfigurator.configure(logConfig);

		if (args.length != 1) {
			System.out
					.println("Usage: java -mx1024M org.fbk.it.hlt.jlsi.data.DenseTextVectorFileReader file");
			System.exit(1);
		}

		File file = new File(args[0]);

		DenseTextVectorFileReader matrixFileReader = new DenseTextVectorFileReader(
				file);
		DoubleMatrix1D matrix = (DoubleMatrix1D) matrixFileReader.read();

		logger.debug("matrix: " + matrix.size());
		for (int i = 0; i < matrix.size(); i++) {
			System.out.print(matrix.getQuick(i) + " ");
		} // end for i

		long end = System.currentTimeMillis();
		System.out.println("matrix wrote in " + (end - begin) + " ms");
	} // end main

} // end DenseTextVectorFileReader